# -*- coding: utf-8 -*-
# @Time: 2021/8/16 8:29
# @File: problem-19.py
# @DESC: https://leetcode-cn.com/problems/remove-nth-node-from-end-of-list/
# 删除链表倒数N个节点
# Definition for singly-linked list.
from list_node_common import *


class Solution:
    def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
        cur = head
        node_list = []
        while cur:
            node_list.append(cur)
            cur = cur.next
        lent = len(node_list)
        if lent == 1:
            return None
        #倒数lent
        if lent == n:
            return node_list[1]
        else:
            t = node_list[lent - n - 1]
            if n != 1:
                r = node_list[lent - n + 1]
            else:
                r = None
            t.next = r
        return head


data = [1, 2]

l = listToNode(data)

solution = Solution()
result = solution.removeNthFromEnd(l, 2)
print()
